// 个人中心相关 API

import request from './index';
import type { UserProfile, Bookmark } from '@/types/profile';
import type { ApiResponse } from '@/types/api';
import { 
  mockGetUserProfile, 
  mockAddBookmark, 
  mockDeleteBookmark,
  mockGetBookmarks
} from '@/mock/profile';

const useMock = import.meta.env.VITE_USE_MOCK === 'true';

/**
 * 获取用户档案
 */
export const getUserProfile = async (): Promise<ApiResponse<UserProfile>> => {
  if (useMock) {
    return mockGetUserProfile();
  }
  return request.get('/user/profile');
};

/**
 * 添加收藏
 */
export const addBookmark = async (bookmark: Omit<Bookmark, 'id' | 'createdAt'>): Promise<ApiResponse<Bookmark>> => {
  if (useMock) {
    return mockAddBookmark(bookmark);
  }
  return request.post('/user/bookmarks', bookmark);
};

/**
 * 删除收藏
 */
export const deleteBookmark = async (bookmarkId: string): Promise<ApiResponse<null>> => {
  if (useMock) {
    return mockDeleteBookmark(bookmarkId);
  }
  return request.delete(`/user/bookmarks/${bookmarkId}`);
};

/**
 * 获取收藏列表
 */
export const getBookmarks = async (category?: string): Promise<ApiResponse<{ bookmarks: Bookmark[] }>> => {
  if (useMock) {
    return mockGetBookmarks(category);
  }
  const params = category ? { category } : {};
  return request.get('/user/bookmarks', { params });
};